package com.peas.platform.module.icims;

import com.peas.platform.module.icims.base.sys.dto.RecycleDto;
import com.peas.platform.module.icims.base.sys.service.RecycleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RecycleInsertTestCase {

    @Resource
    private RecycleService recycleService;

    @Test
    public void insertAsync() {
        List<Integer> splitSizes = Arrays.asList(1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 50, 1);
        for (int splitSize : splitSizes) {
            long time = saveAsync(1000, splitSize);
            System.out.println("splitSize:" + splitSize + ",used:" + time);
        }
    }

    @Test
    public void insert() {
        List<Integer> size = Arrays.asList(1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 50);
        List<Long> list1 = new ArrayList<>();
        List<Long> list2 = new ArrayList<>();
        for (int s : size) {
            System.out.println("save1:" + s);
            list1.add(save1(s));
            System.out.println("save2:" + s);
            list2.add(save2(s));
        }
        System.out.println(list1);
        System.out.println(list2);
    }

    private long save1(int num) {
        List<RecycleDto> inserts = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            RecycleDto dto = new RecycleDto();
            dto.setContent("save1_" + i);
            inserts.add(dto);
        }
        StopWatch sw = new StopWatch();
        sw.start();
        recycleService.saveBatch1(inserts);
        sw.stop();
        return sw.getLastTaskTimeMillis();
    }

    private long save2(int num) {
        List<RecycleDto> inserts = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            RecycleDto dto = new RecycleDto();
            dto.setContent("save2_" + i);
            inserts.add(dto);
        }
        StopWatch sw = new StopWatch();
        sw.start();
        recycleService.saveBatch2(inserts);
        sw.stop();
        return sw.getLastTaskTimeMillis();
    }

    private long saveAsync(int num, int splitSize) {
        List<RecycleDto> inserts = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            RecycleDto dto = new RecycleDto();
            dto.setContent("content_" + i);
            inserts.add(dto);
        }
        StopWatch sw = new StopWatch();
        sw.start();
        recycleService.saveBatchAsync1(inserts, splitSize);
        sw.stop();
        return sw.getLastTaskTimeMillis();
    }

}
